﻿using B.S.Material.Domain.BOM;
using B.S.Material.ErrorCode;
using B.S.Material.Read.Api.Applications.Command.FeedDetailsCommand;
using B.S.Material.Read.Api.Applications.Command.FeedingCommand;
using B.S.Material.Read.Api.Applications.Command.MaterialsChildCommand;
using B.S.Material.Read.Api.Applications.Command.MaterialsCommnad;
using B.S.Material.Read.Api.Applications.Command.MaterialsPickingCommand;
using B.S.Material.Read.Api.Applications.Command.MaterialsPickingDetailCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SqlSugar;

namespace B.S.Material.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GetValuesController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ISqlSugarClient db;
        public GetValuesController(IMediator mediator, ISqlSugarClient db)
        {
            this.mediator = mediator;
            this.db = db;
        }
        /// <summary>
        /// 物料管理主表反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Materials>>> FTMaterials([FromQuery] FTMaterialsCommnad request)
        {
            return await mediator.Send(request);
        }
        [HttpGet]
        public async Task<ApiResult<List<Materials>>> ZXSDMaterials([FromQuery] XSDMaterialsCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 物料管理主表反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<Materials>>> GetMaterials([FromQuery] GetMaterialsCommnad request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 物料管理子表反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialsChild>>> FTMaterialsChild([FromQuery] FTMaterialsChildCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 物料管理子表查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<MaterialsChild>>> GetMaterialsChild([FromQuery] GetMaterialsChildCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 导出方法
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public ActionResult ExportToExcel()
        {
            try
            {
                // 模拟查询数据，这里假设查询 User 表
                var data = db.Queryable<Materials>().OrderBy(x => x.CreationTime).ToList();

                // 创建一个新的 Excel 工作簿
                IWorkbook wb = new XSSFWorkbook();

                //在Excel文件中创建工作表
                ISheet sheet = wb.CreateSheet("计划表");

                //在工作表中创建行(第一行,标题);
                IRow row = sheet.CreateRow(0);

                //创建单元格
                row.CreateCell(0).SetCellValue("主键");
                row.CreateCell(1).SetCellValue("物料清单主题");
                row.CreateCell(2).SetCellValue("物料清单编号");
                row.CreateCell(3).SetCellValue("制单人员");
                row.CreateCell(4).SetCellValue("备注");
                row.CreateCell(5).SetCellValue("产品名称");
                row.CreateCell(6).SetCellValue("产品编号");
                row.CreateCell(7).SetCellValue("规格型号");
                row.CreateCell(8).SetCellValue("单位");
                row.CreateCell(9).SetCellValue("数量");
                row.CreateCell(10).SetCellValue("产品角色");
                row.CreateCell(11).SetCellValue("工艺流程");
                row.CreateCell(12).SetCellValue("耗损率(%)");
                row.CreateCell(13).SetCellValue("保存时间");
                row.CreateCell(14).SetCellValue("是否删除");

                var index = 1;
                foreach (var item in data)
                {
                    row = sheet.CreateRow(index);

                    row.CreateCell(0).SetCellValue(item.BOMID.ToString());
                    row.CreateCell(1).SetCellValue(item.BOMSubject);
                    row.CreateCell(2).SetCellValue(item.DocumentNo);
                    row.CreateCell(3).SetCellValue(item.Creator);
                    row.CreateCell(4).SetCellValue(item.Remarks);
                    row.CreateCell(5).SetCellValue(item.ProductName);
                    row.CreateCell(6).SetCellValue(item.ProductCode);
                    row.CreateCell(7).SetCellValue(item.SpecificationModel);
                    row.CreateCell(8).SetCellValue(item.Unit);
                    row.CreateCell(9).SetCellValue(item.Quantity.ToString());
                    row.CreateCell(10).SetCellValue(item.ProductRole);
                    row.CreateCell(11).SetCellValue(item.ProcessFlow);
                    row.CreateCell(12).SetCellValue(item.LossRate.ToString());
                    row.CreateCell(13).SetCellValue(item.CreationTime.ToString("yyyy-MM-dd"));
                    row.CreateCell(14).SetCellValue(item.IsDel ? "删除" : "未删除");

                    index++;
                }

                //Excel的值
                byte[] buffer;
                //通过内存流读出Excel内容,转换为二进制数组
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }

                //导出Excel  using  Microsoft.AspNetCore.StaticFiles;
                string excelMime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                //导出
                return File(buffer, excelMime, "计划 表.xlsx");
            }
            catch (Exception ex)
            {
                // 处理异常
                Console.WriteLine($"导出 Excel 时出错: {ex.Message}");
            }

            return new EmptyResult();
        }
        /// <summary>
        /// 生产领料主显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<MaterialsPicking>>> GetMaterialsPicking([FromQuery] GetMaterialsPickingCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产领料主显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialsPicking>>> FTMaterialsPicking([FromQuery] FTMaterialsPickingCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产领料子显反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialsPickingDetail>>> Handle([FromQuery] ftMaterialsPickingDetailCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产领料子显反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialsPickingDetail>>> GetMaterialsPickingDetail([FromQuery] GetMaterialsPickingDetailCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 导出方法
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public ActionResult ExportToExce2()
        {
            try
            {
                // 模拟查询数据，这里假设查询 User 表
                var data = db.Queryable<MaterialsPicking>().ToList();

                // 创建一个新的 Excel 工作簿
                IWorkbook wb = new XSSFWorkbook();

                //在Excel文件中创建工作表
                ISheet sheet = wb.CreateSheet("生产领料表");

                //在工作表中创建行(第一行,标题);
                IRow row = sheet.CreateRow(0);

                //创建单元格
                row.CreateCell(0).SetCellValue("主键");
                row.CreateCell(1).SetCellValue("单据主题");
                row.CreateCell(2).SetCellValue("单据编号");
                row.CreateCell(3).SetCellValue("领料日期");
                row.CreateCell(4).SetCellValue("来源单据类型");
                row.CreateCell(5).SetCellValue("来源单据");
                row.CreateCell(6).SetCellValue("领料数量合计");
                row.CreateCell(7).SetCellValue("制单日期");
                row.CreateCell(8).SetCellValue("制单人员");
                row.CreateCell(9).SetCellValue("关联项目");
                row.CreateCell(10).SetCellValue("备注");

                var index = 1;
                foreach (var item in data)
                {
                    row = sheet.CreateRow(index);

                    row.CreateCell(0).SetCellValue(item.PickingId.ToString());
                    row.CreateCell(1).SetCellValue(item.DocumentSubject);
                    row.CreateCell(2).SetCellValue(item.DocumentNo);
                    row.CreateCell(3).SetCellValue(item.PickingDate);
                    row.CreateCell(4).SetCellValue(item.DocumentType);
                    row.CreateCell(5).SetCellValue(item.DocumentSource);
                    row.CreateCell(6).SetCellValue(item.PickingNumber.ToString());
                    row.CreateCell(7).SetCellValue(item.ProduceDate.ToString("yyyy-MM-dd"));
                    row.CreateCell(8).SetCellValue(item.ProduceName);
                    row.CreateCell(9).SetCellValue(item.RelatedProject);
                    row.CreateCell(10).SetCellValue(item.PickingNote);
                    row.CreateCell(14).SetCellValue(item.IsDel ? "删除" : "未删除");
                    index++;
                }

                //Excel的值
                byte[] buffer;
                //通过内存流读出Excel内容,转换为二进制数组
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }

                //导出Excel  using  Microsoft.AspNetCore.StaticFiles;
                string excelMime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                //导出
                return File(buffer, excelMime, "计划 表.xlsx");
            }
            catch (Exception ex)
            {
                // 处理异常
                Console.WriteLine($"导出 Excel 时出错: {ex.Message}");
            }

            return new EmptyResult();
        }
        /// <summary>
        /// 生产补料显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<Feedings>>> GETFeeding([FromQuery] GetFeedingCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产补料反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Feedings>>> FTFeeding([FromQuery] FTFeedingCommand requestn)
        {
            return await mediator.Send(requestn);
        }

        /// <summary>
        /// 生产补料子显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FeedDetailss>>> GetFeedDetailss([FromQuery] GetFeedDetailsCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产补料子反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FeedDetailss>>> FTFeedDetailss([FromQuery] FTFeedDetailsCommand request)
        {
            return await mediator.Send(request);
        }




    }
}
